package vip.yugu.interview;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@Service
public class InterviewService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private InterviewMapper interviewMapper;

    @PostConstruct
    private void init() {
        // http://xxfb.mwr.cn/sq_zdysq.html
        // http://xxfb.mwr.cn/hydroSearch/pointHydroInfo
        interviewMapper.clearAll();
        Map<Object, Object> map = new RestTemplate().postForObject("http://xxfb.mwr.cn/hydroSearch/pointHydroInfo", null, Map.class);
        Map<String, Object> result = (Map<String, Object>) map.get("result");
        List<Map<String, Object>> data = (List<Map<String, Object>>) result.get("data");
        List<Interview> list = data.stream().map(item -> new Interview(
                null,
                ((String) item.get("poiBsnm")).trim(),
                (String) item.get("poiAddv"),
                ((String) item.get("rvnm")).trim(),
                ((String) item.get("stnm")).trim(),
                (String) item.get("dateTime"),
                ((Double) item.get("dyp")).floatValue(),
                (String) item.get("wth")
        )).toList();
        interviewMapper.batchInsert(list);
    }

    public List<Interview> indexData() {
        return interviewMapper.findAdd();
    }


}
